#ifndef THREAD_POOL_CLASS
#define THREAD_POOL_CLASS

#include <windows.h>
#include <types.h>
#include <list.h>

#ifdef COMMONFUNC_EXPORTS
#define THREADPOOL_EXPORT __declspec(dllexport)
#else
#define THREADPOOL_EXPORT __declspec(dllimport)
#endif

enum ThreadPriority {High, Low};
enum PoolState {Ready, Destroying, Destroyed};

struct Task;
struct Thread;
class THREADPOOL_EXPORT ThreadPool 
{
public:
    ThreadPool(int pool_size, int wait_for_dead_ms);
    virtual ~ThreadPool();

    int	run(LPTHREAD_START_ROUTINE proc, void *data, ThreadPriority priority);

    PoolState get_state();

private:
    int	 pool_size;
    int	 wait_for_dead_ms;

    HANDLE fd_shutdown;

    CRITICAL_SECTION cs_task_queue;
    CRITICAL_SECTION cs_thread_queue;

    struct list_head thread_queue;
    struct list_head task_queue;

    volatile PoolState state;

    int	create();
    void destroy();

    void finish_notify(u32 id);
    int check_state_stop(void);

    static u32 __stdcall thread_proc(void *data);

    int	get_working_thread_count();

    struct Task* get_task_from_queue(void);
    struct Thread* get_thread_from_queue(u32 id);
};
//------------------------------------------------------------------------------
#endif
